package cn.kgc.vue.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.kgc.vue.anno.LogAnnotation;
import cn.kgc.vue.common.Operation;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.DeptPageDTO;
import cn.kgc.vue.entity.Dept;
import cn.kgc.vue.entity.User;
import cn.kgc.vue.service.DeptService;
import cn.kgc.vue.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 课工场
 * @since 2023/8/15
 */
@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private UserService userService;

    // 部门列表
    @GetMapping("list")
    public ResponseResult list() {
        List<Dept> list = deptService.list();
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS, list);
        return responseResult;
    }

    // 部门分页信息
    @GetMapping("page")
    public ResponseResult getPage(DeptPageDTO deptPageDTO) {

        return deptService.getPage(deptPageDTO);
    }

    // 添加修改部门信息
    @PostMapping("saveOrUpdate")
    @LogAnnotation(model = "部门模块",operation = Operation.UPDATE)
    @RequiresPermissions(value = {"sys:dept:add","sys:dept:update"},logical = Logical.OR)
    public ResponseResult saveOrUpdate(@RequestBody Dept dept) {
        ResponseResult responseResult = new ResponseResult();
        // 判定部门名称是否重复
        String deptName = dept.getDeptName();
        LambdaQueryWrapper<Dept> lambda = new QueryWrapper<Dept>().lambda();
        lambda.eq(Dept::getDeptName,deptName);
        Dept one = deptService.getOne(lambda);
        if (ObjectUtil.isNotEmpty(one)){
            responseResult.setMsg("部门名称已存在");
            responseResult.setCode(500);
            return responseResult;
        }
        deptService.saveOrUpdate(dept);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }

    //根据id 获取部门信息
    @GetMapping("getById")
    public ResponseResult getDeptInfo(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        Dept dept = deptService.getById(id);
        responseResult.success(ResponseStatus.SUCCESS,dept);
        return responseResult;
    }

    //根据id 删除部门信息
    @DeleteMapping("delete")
    @LogAnnotation(model = "部门模块",operation = Operation.DELETE)
    @RequiresPermissions(value = {"sys:dept:delete"})
    public ResponseResult deleteDept(Integer id) {

        ResponseResult responseResult = new ResponseResult();
        //判定部门是否包含用户信息
        LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
        lambda.eq(User::getDeptId,id);
        List<User> list = userService.list(lambda);
        if (ObjectUtil.isNotEmpty(list)){
            responseResult.setCode(500);
            responseResult.setMsg("不能删除该部门");
            return  responseResult;
        }

        deptService.removeById(id);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }
}
